import { FormInstance } from "element-plus"
import {get, post, put} from "../utils/request"
import {ref, reactive,getCurrentInstance} from "vue"


export default function () {

    const { proxy } = getCurrentInstance();

    const queryParam = reactive({page: 1 as Number, size: 10})

    const data = reactive({loading: false,saveLoading: false,baseUrl: '', queryParam: {page: 1, size: 10},selections: [],showSearch: true, editDialogShow: false, viewDialogShow: false, formData: {},rules: {}})
    const baseUrl = ref('')
    const total = ref(0)
    const dataList = ref([])
    const  isEdit=ref(false)

    function initList(url) {
        data.loading = true
        if (url) {
            data.baseUrl = url
        }
        get(data.baseUrl, data.queryParam).then(res => {
            data.loading = false
            // @ts-ignore
            total.value = parseInt(res.totalElements)
            // @ts-ignore
            dataList.value = res.content
        })
    }

    function  selectionChange(e){
        data.selections=e
    }

    function toView(row:Object,formName:String){
        data.viewDialogShow =true
        formName=formName?formName:'form'
        proxy.$nextTick(()=>{
            const  formRef=proxy.$refs[formName+''] as FormInstance
            setTimeout(function (){
                formRef.resetFields()
                data.formData=Object.assign({},row?row:{})
            },100)
        })
    }

    function toEdit(row:Object,formName:String){
         isEdit.value=true;
         toOperate(row,formName)
    }

    function toAdd(row:Object,formName:String){
        isEdit.value=false;
        toOperate(row,formName)
    }

    function  toOperate(defaultValue:Object,formName:String){
        data.editDialogShow =true
        formName=formName?formName:'form'
        proxy.$nextTick(()=>{
            const  formRef=proxy.$refs[formName+''] as FormInstance
            setTimeout(function (){
                formRef.resetFields()
                data.formData=Object.assign({},defaultValue?defaultValue:{})
            },100)
        })

    }

    function toSave(formName:String,callback:Function,validCallback){
        formName=formName?formName:'form'
        const  form=proxy.$refs[formName+''] as FormInstance
        form.validate((valid, fields) => {
            if(valid){
                data.saveLoading=true
                let method=post
                if(isEdit.value){
                    method= put
                }
                var methodData=data.formData
                if(validCallback){
                    methodData=validCallback()
                }
                method(data.baseUrl,methodData).then(res=>{
                    data.saveLoading=false
                    data.editDialogShow=false
                    if(callback){
                        callback()
                    }else {
                        initList(null)
                    }
                }).catch(err=>{
                    data.saveLoading=false
                })
            }else {
                console.log('验证失败')
            }
        })

    }

    return {
        initList,
        selectionChange,
        toAdd,
        toEdit,
        toSave,
        toView,
        queryParam,
        total,
        dataList,
        data,
        baseUrl
    }
}
